package native

import (
	"jvm/rtda"
)

type NativeMethod func(jFrame *rtda.JFrame)

type NaMethod struct {
	MethodName       string
	MethodDescriptor string
	Method           NativeMethod
}

var registry = map[string]NativeMethod{}

func Register(className string, nm *NaMethod) {
	key := className + "~" + nm.MethodName + "~" + nm.MethodDescriptor
	registry[key] = nm.Method
}

func Registers(className string, nms []*NaMethod) {
	for _, nm := range nms {
		Register(className, nm)
	}
}

func FindNativeMethod(className string, methodName string, methodDescriptor string) NativeMethod {
	key := className + "~" + methodName + "~" + methodDescriptor
	if method, ok := registry[key]; ok {
		return method
	}
	if methodDescriptor == "()V" && (methodName == "registerNatives" || methodName == "initIDs") {
		return emptyNativeMethod
	}
	return nil
}

func emptyNativeMethod(frame *rtda.JFrame) {
	//do nothing
}
